﻿using System;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace NTmdb.TestProject
{
    /// <summary>
    ///     This is a test class for TaskTmdbResultExtensionTest and is intended
    ///     to contain all TaskTmdbResultExtensionTest Unit Tests
    /// </summary>
    [TestClass]
    public class TaskTmdbResultExtensionTest
    {
        private static async Task<TmdbResult<String>> TestMethod( String result, Exception ex = null, TmdbStatusResponse apiError = null )
        {
            await Task.Delay( 100 );
            return new TmdbResult<String>
                {
                    Result = result,
                    Error = ex,
                    ApiErrorResponse = apiError
                };
        }

        [TestMethod]
        public async Task UnwrapTest()
        {
            var result = Guid.NewGuid().ToString();
            var actual = await TestMethod( result ).Unwrap();
            Assert.AreEqual( result, actual );
        }

        [TestMethod]
        public async Task UnwrapTest1()
        {
            var result = Guid.NewGuid().ToString();
            var actual = await TestMethod( result, new Exception( "Test" ) ).Unwrap();
            Assert.IsNull( actual );
        }

        [TestMethod]
        public async Task UnwrapOrThrowTest()
        {
            var result = Guid.NewGuid().ToString();
            var actual = await TestMethod( result ).UnwrapOrThrow();
            Assert.AreEqual( result, actual );
        }

        [TestMethod]
        public async Task UnwrapOrThrowTest1()
        {
            var apiErrorResponse = new TmdbStatusResponse();
            var result = Guid.NewGuid().ToString();
            var exception = new NullReferenceException();

            var exceptionThrown = false;
            try
            {
                var actual = await TestMethod( result, exception, apiErrorResponse ).UnwrapOrThrow();
            }
            catch ( TmdbException ex )
            {
                exceptionThrown = true;
                Assert.AreSame( exception, ex.InnerException );
                Assert.AreSame( apiErrorResponse, ex.ApiError );
            }
            Assert.IsTrue( exceptionThrown );
        }
    }
}